<?php

namespace app\common\command;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\Children;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\user\WalletLog;
use app\common\model\shopro\ReleaseAccelerateLogModel;
use app\common\model\shopro\ReleaseAccelerateMarketplaceLogModel;
use app\common\model\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Jicharelease extends Command
{
    protected function configure()
    {
        $this->setName('jicharelease')->setHelp('市场补贴励')->setDescription('市场补贴励');

    }

    protected function execute(Input $input, Output $output)
    {
        $is_release = config('site.is_release');
        if ($is_release == 1) {
            ini_set('memory_limit', "-1");
            $accelerateLogModel = new ReleaseAccelerateMarketplaceLogModel();
            $count              = $accelerateLogModel->where(['shifang_type'=>1])->whereTime('createtime', 'd')->count();
            $data               = db('agentreward_log')->whereTime('createtime', 'd')->select();
            if(config('site.tiaoshi') == 1){
                $count = 0;
            }
            if (!$count && $data) {
                $inser             = [];
                    Db::startTrans();
                    try {
                        $arr  = [];
                        $accelerateLogModel = new ReleaseAccelerateMarketplaceLogModel();
                        foreach ($data as $k => $v) {
                            if (!isset($arr[$v['user_id'].'-'.$v['type']])) {
                                $arr[$v['user_id'].'-'.$v['type']]               = $v;
                            } else {
                                $arr[$v['user_id'].'-'.$v['type']]['amount'] += $v['amount'];
                            }
                        }
                        $jichaArray = [];
                        $pingjiArray = [];
                        $key = -1;
                        foreach ($arr as $kk => $vv) {
                            $key ++;
                            if ($vv['type'] == 1) {
                                $accelerate_scale_two = $vv['accelerate_scale_two']??0;
                                $accelerate_scale = ($vv['accelerate_scale']-$accelerate_scale_two)/100;
                                $amount = bcmul($vv['amount'], $accelerate_scale,2);//市场补贴励
                                $jichaArray[$key]['user_id'] = $vv['user_id'];
                                $jichaArray[$key]['amount'] = $amount;
                            }else{
                                $pingjiArray[$key]['user_id'] = $vv['user_id'];
                                $pingjiArray[$key]['amount'] = bcmul($vv['amount'], 0.01,2);;
                            }
                        }
                        if ($jichaArray) {
                            $inser = $this->accelerateshifang($jichaArray,'jicha');
                            if ($inser['result']) {
                                unset($inser['result']);
                                $inser['shifang_type'] = 1;
                                $accelerateLogModel->create($inser);
                            }
                        }
                        if ($pingjiArray) {
                            $inser = $this->accelerateshifang($pingjiArray,'pingji');
                            if ($inser['result']) {
                                unset($inser['result']);
                                $inser['shifang_type'] = 2;
                                $accelerateLogModel->create($inser);
                            }
                        }
                        db('agentreward_log')->whereTime('createtime', 'd')->delete();
                        Db::commit();
                    } catch (HttpResponseException $e) {
                        Db::rollback();
                        $data    = $e->getResponse()->getData();
                        $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                        format_log_error($e, 'Release.release.HttpResponseException', $message);
                    }
                    if ($inser) {
                        echo "释放成功";
                    }
            } else {
                echo "今日已释放";
            }
        }
    }

    /**
     * @menu 释放
     * @Author:男人的坏
     * @Date:2024-04-16 16:49
     */
    public function accelerateshifang($data,$event)
    {

        $golen_scale = config('site.golen_scale') / 100;        //金豆比例
        $slive_scale = (100 - config('site.golen_scale')) / 100;//银豆比例
        $userModel   = new \app\common\model\User();
        $result      = false;
        $all_score   = 0;
        $all_golden  = 0;
        $all_silver  = 0;
        $user_num    = 0;
        $user_ids    = '';
        if($event  == 'pingji'){
            $memo = '平级奖励';
        }else{
            $memo = '市场补贴励';
        }
        foreach ($data as $k => $v) {
            $userInfo = $userModel->where(['id' => $v['user_id']])->field('id,score,is_old')->find();
            if($userInfo->score > 0){
                $release  = $v['amount'];
                if ($userInfo->score <= $release) {
                    $release = $userInfo->score;
                }
                $all_score += $release;

                $user_num++;
                $user_ids   .= ','.$v['user_id'];
                $result     = WalletService::change($v['user_id'], 'score', -$release, $event, [],
                    $memo);
                $golden     = bcmul($release, $golen_scale, 2);
                $all_golden += $golden;
                $result     = WalletService::change($v['user_id'], 'golden', $golden, $event, [],
                    $memo.'积分转化金豆');
                $slive      = bcmul($release, $slive_scale, 2);
                $all_silver += $slive;
                $result     = WalletService::change($v['user_id'], 'silver', $slive,  $event, [],
                    $memo.'积分转化银豆');
            }

        }

        $arr['result']     = $result;
        $arr['user_ids']   = trim($user_ids, ',');
        $arr['user_num']   = $user_num;
        $arr['all_golden'] = $all_golden;
        $arr['all_silver'] = $all_silver;
        $arr['all_score']  = $all_score;
        return $arr;
    }
}
